package com.market.domain;

import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.faces.context.FacesContext;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.market.domain.transients.Email;
@Entity
@Table(name = "feedback")
public class FeedBack extends BaseObject {

	/**
	 * 
	 */
	private static final long serialVersionUID = -9128203576119748448L;
	public enum TYPE {
		DROP_OFF, ORDERING, DELIVERY, PAYMENT,REFUND, QUALITY,REGISTERING,LOGIN,OTHER;

		private String getMessage(String key) {
			Locale locale = FacesContext.getCurrentInstance().getViewRoot()
					.getLocale();
			java.util.ResourceBundle resBundle = ResourceBundle.getBundle(
					"com.market.messages", locale);
			return resBundle.getString(key);
		}

		public String getLabel() {
			return this.getMessage("FEEDBACK_TYPE_" + this.toString());
		}
	}
	
	@Override
	public boolean equalsId(Object o) {
		if ((o instanceof FeedBack) && ((FeedBack) o).getId().equals(id)) {
			return true;
		}
		return false;
	}
	
	
	@Transient
	public Email getAnswerEmail() {
		Email answerEmail = new Email();
		answerEmail.setTo(this.getEmail());
		answerEmail.setWebSiteName(super
				.getMessage("webSiteName"));
		answerEmail.setFrom(this.getMessage("ourEmail"));
		answerEmail.setSubject(this.getAnswerTitle());
		String text = this.getAnswers();
		text = text.replace("\n","<br/>");
		answerEmail.setContent(text);
		return answerEmail;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id = 0L;
	@Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
	private Date feedBackedDate;
	@Column(length = 150)
	private String userName;
	@Column(length = 150)
	private String email;
	@Column(length = 50)
	private String telephone;
	@Column(length = 200)
	private String title;
	@Column(length = 300)
	private String comments;
	@Column(length = 1500)
	private String answerTitle;
	@Column(length = 1500)
	private String answers;
	@Column(nullable = false, columnDefinition = "bit(1) DEFAULT 1")
	private Boolean isRead = Boolean.FALSE;
	@Enumerated(EnumType.STRING)
	private TYPE type = TYPE.DROP_OFF;
	
	
	public Long getId() {
		return id;
	}
	
	public void setId(Long id) {
		this.id = id;
	}
	
	public Date getFeedBackedDate() {
		return feedBackedDate;
	}
	public void setFeedBackedDate(Date feedBackedDate) {
		this.feedBackedDate = feedBackedDate;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getTelephone() {
		return telephone;
	}
	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}
	
	
	public Boolean getIsRead() {
		return isRead;
	}
	public void setIsRead(Boolean isRead) {
		this.isRead = isRead;
	}
	
	
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getComments() {
		return comments;
	}
	public void setComments(String comments) {
		this.comments = comments;
	}
	
	
	
	public String getAnswerTitle() {
		return answerTitle;
	}


	public void setAnswerTitle(String answerTitle) {
		this.answerTitle = answerTitle;
	}


	public String getAnswers() {
		return answers;
	}

	public void setAnswers(String answers) {
		this.answers = answers;
	}

	public TYPE getType() {
		return type;
	}
	public void setType(TYPE type) {
		this.type = type;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((comments == null) ? 0 : comments.hashCode());
		result = prime * result + ((email == null) ? 0 : email.hashCode());
		result = prime * result + ((userName == null) ? 0 : userName.hashCode());
		result = prime * result
				+ ((telephone == null) ? 0 : telephone.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		FeedBack other = (FeedBack) obj;
		if (comments == null) {
			if (other.comments != null)
				return false;
		} else if (!comments.equals(other.comments))
			return false;
		if (email == null) {
			if (other.email != null)
				return false;
		} else if (!email.equals(other.email))
			return false;
		if (userName == null) {
			if (other.userName != null)
				return false;
		} else if (!userName.equals(other.userName))
			return false;
		if (telephone == null) {
			if (other.telephone != null)
				return false;
		} else if (!telephone.equals(other.telephone))
			return false;
		return true;
	}
	@Override
	public String toString() {
		return "FeedBack [id=" + id + ", name=" + userName + ", email=" + email
				+ ", telephone=" + telephone + ", content=" + comments + "]";
	}
		
	
}
